<!--
  移动签批》发文》（底部）更多操作》会签管理
-->
<template>
  <view>
    <van-tabs color="#1989fa" title-active-color="#1989fa" animated v-model:active="active">
      <van-tab title="index" v-for="(item, index) in tabList" :title="item.tabName">
        <van-list v-if="index==0" v-model="loading" :finished="finished" @load="loadUnConSign">
          <toSendList @deleteConSign="deleteConSign" @sendHuiqian="sendHuiqian" ref="uncon-signList"
                      :paramObject="paramObject" :unConSignList="unConSignList"/>
        </van-list>
        <van-list v-else v-model="loading" :finished="finished" @load="loadConSign">
          <sendList @revokeHuiqian="revokeHuiqian" @recoverHuiqian="recoverHuiqian" ref="con-signList"
                    :conSignList="conSignList"/>
        </van-list>
      </van-tab>
    </van-tabs>
  </view>
</template>

<script>
import sendList from 'pages/admin/components/sendDoc/compo-sendList.vue'
import toSendList from 'pages/admin/components/sendDoc/compo-toSendList.vue'

export default {
  components: {
    sendList,
    toSendList,
  },
  //获取上个界面传过来的参数
  onLoad: function (option) {
    this.userInfo = this.cache.get('userInfo')//从缓存获取用户信息userInfo: {"systemKey":"ls_liu","systemFlag":"yidong"}
    //从地址栏获取proInstId
    this.proInstId = option.proInstId
    this.fwId = option.fwId
    this.docTitle = option.docTitle
    this.paramObject.proInstId = this.proInstId
    this.paramObject.fwId = this.fwId
  },
  mounted() {
    this.loadConSign()//加载已发送会签单位
    this.loadUnConSign()//加载待发送会签单位
    this.getCaacDetail(this.proInstId)//详情
  },
  onShow() {
    this.listHuiqianInfo(this.fwId)
  },
  data() {
    return {
      userInfo: {},//当前登录用户信息，目前先从缓存中获取
      paramObject: {//待发送会签单位-添加更多会签单位所需请求参数
        proInstId: '',
        fwId: '',
        param01: {},//获取签批功能菜单所需要的参数
      },
      nextActivities: [],//发送会签所需参数，调用listNextActivities接口获得
      unConSignList: [],//待发送会签单位列表
      proInstId: '',
      fwId: '',//发文id
      docTitle:'',//首页传过来的公文标题
      active: 0,
      tabList: [{
          tabName: '待发送会签单位'
        }, {
          tabName: '已发送会签单位'
        },
      ],
      checked: true,
      conSignList: [],//已发送会签单位
      loading: true,
      finished: false,
      fwForm: {},
      huiqianUser:'',//会签选择的id
      limitTimeDays:''
    }
  },
  methods: {
    // 加载已发送会签单位列表
    loadConSign() {
      this.listSendedHuiqianTrack(this.proInstId)
    },

    // 加载待发送会签单位列表
    loadUnConSign() {
      this.listHuiqianInfo(this.fwId)
    },

    // 多选
    selectionChange(e) {
      this.selectedIndexs = e.detail.index
    },

    // 获取已发送会签单位
    listSendedHuiqianTrack(proInstId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/mhjcomponent/countersign/countersignController/listSendedHuiqianTrack`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
          'proInstId': proInstId
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.loading = false;
          this.conSignList = data.data
          this.finished = true;
        } else {
          console.log("获取已发送会签单位信息失败：code"+data.code)
        }

      })
    },

    // 获取待发送会签单位
    listHuiqianInfo(fwId) {
      this.$httploading({
        url: this.$http.adornUrl(`/caac/oa/mhjcomponent/countersign/countersignController/listHuiqianInfo`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
          'gwId': fwId,
          'state': "待会签"
        })
      }).then(({data}) => {
        console.log("待发送会签单位: " + JSON.stringify(data));
        if (data.code == 200) {
          this.loading = false;
          this.unConSignList = data.data
          this.finished = true;
        } else {
          console.log("获取待发送会签单位信息失败：code"+data.code)
        }
      })
    },

    // 删除待发送会签单位
    deleteConSign(data) {
      var uuids = data.uuids
      if (uuids.length > 0) {
        for (var i = 0; i < uuids.length; i++) {
          this.deleteConSignByUuid(uuids[i])
        }
      }

    },
    // 根据uuid删除待发送会签单位
    deleteConSignByUuid(uuid) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/mhjcomponent/countersign/countersignController`),
        method: 'delete',
        params: this.$http.adornParams({
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
          'delId': uuid
        })
      }).then(({data}) => {
        this.listHuiqianInfo(this.fwId)
        if (data.code == 200) {
          this.$toast('删除成功')
        } else {
          console.log("删除会签单位信息失败：code"+data.code)
        }
      })
    },
    // 添加待发送会签单位，子组件传过来值进行增加
    updateCountersigning(countersignValue) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/mhjcomponent/countersign/countersignController/updateCountersigning`),
        method: 'POST',
        params: this.$http.adornParams({
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
          'fwId': this.fwId,
          'countersignValue': countersignValue
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.$toast(JSON.stringify(data.data))
          this.listHuiqianInfo(this.fwId)//刷新待发送会签单位列表，更新
        } else {
          console.log(data);
        }

      })
    },

    // 发送会签
    sendHuiqian(sendHuiqianObj) {
      //{"receiverIdArr":["3887"],"limitDays":""}
      this.huiqianUser = sendHuiqianObj.receiverIdArr.toString()
      this.limitTimeDays = sendHuiqianObj.limitDays
      this.$http({
        url: this.$http.adornUrl(`/caac/wfm/ApplicationController/listNextActivities`),
        method: 'post',
        params: {
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
          'data': this.$http.adornData({
            'wfmData': {
              'configId': this.paramObject.param01.configId,
              'proDirId': this.paramObject.param01.proDirId,
              'actDefId': this.paramObject.param01.actDefId,//
              'actInstId': this.paramObject.param01.actInstId,//
              'workitemId': this.paramObject.param01.workitemId,
              'wfmInitPageType': 'running'
            }
          })
        }
      }).then(({data}) => {
        if (data.code == 200) {
          let nextActDefIds = data.data.nextActDefIds;
          if (nextActDefIds != undefined && nextActDefIds.length > 0) {
            for (var i = 0; i < nextActDefIds.length; i++) {
              if (nextActDefIds[i].actDefId.indexOf('huiqian') != -1) {//gljhuiqian
                this.nextActivities.push(nextActDefIds[i])
              }
            }
            this.nextActivities[0].participants = this.userInfo.userId //当前用户的userId
            this.completeWorkitem()//第一次发会签调用此接口
          }else{
            //补发会签 （若之前已经发送过会签，再次发送则为补发会签）
            this.addSubflowAfterSuspended()
          }
        } else {
          this.$toast(data.message + ' code:' + data.code)
        }
      })

    },
    //发送会签
    completeWorkitem(){
      this.$httploading({
        url: this.$http.adornUrl(`/caac/wfm/ApplicationController/completeWorkitem`),
        method: 'post',
        params: {
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
          'data': this.$http.adornData({
            'fwForm': this.fwForm,
            'wfmData': {
              'configId': this.paramObject.param01.configId,
              'proDirId': this.paramObject.param01.proDirId,//发文详情返回的proInstId
              'actDefId': this.paramObject.param01.actDefId,//
              'proInstId': this.paramObject.param01.proInstId,
              'actInstId': this.paramObject.param01.actInstId,//
              'workitemId': this.paramObject.param01.workitemId,
              'nextActivities': this.nextActivities
            },
            'nextResourcesId': this.userInfo.userId,
            'huiqianUser':this.huiqianUser,
            'limitTimeDays':this.limitTimeDays
          })
        }
      }).then(({data}) => {
        if (data.code == 200) {
          this.$toast("操作成功")
          // 发送通知
          let messageUserIds = this.huiqianUser;
          let linkUrl = this.configUtil.baseUrl + this.configUtil.basePath + "/#/pages/admin/send/index?proInstId=" + this.paramObject.proInstId;
          this.lx.sendMessage(this.cache.get("appType"), messageUserIds,
              this.docTitle, "发文", linkUrl);
          //操作成功返回详情页
          uni.redirectTo({
            url: '/pages/admin/send/index?proInstId=' + this.proInstId
          })
        } else {
          this.$toast(data.message + ' code:' + data.code)
        }

      })
    },
    //补发会签 （若之前已经发送过会签，再次发送则为补发会签）
    addSubflowAfterSuspended(){
      this.$httploading({
        url: this.$http.adornUrl(`/caac/wfm/MhjApplicationController/addSubflowAfterSuspended`),
        method: 'post',
        params: {
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
          'data': this.$http.adornData({
            'proInstId':this.proInstId,
            'actInstId':this.paramObject.param01.actInstId,
            'title':this.fwForm.title,//fwForm的title
            'gongwenId':this.fwForm.extendIdForRelate,//fwForm的fwId
            'subFlowId':"gljfwhq",//固定值
            'resourceIds':this.userInfo.userId,//当前用户userid
            'otherInfo':"updateCountersign",//固定值
            'huiqianUser':this.huiqianUser,//选中的会签单位
            'limitTimeDays':this.limitTimeDays
          })
        },

      }).then(({data}) => {
        if (data.code == 200) {
          this.$toast("操作成功")
          //操作成功返回详情页
          uni.redirectTo({
            url: '/pages/admin/send/index?proInstId=' + this.proInstId
          })
        } else {
          this.$toast(data.message + ' code:' + data.code)
        }
      })
    },
    /**
     * 撤回会签
     * @param index 子组件传过来的下标，直接通过下标取对应的数据
     */
    revokeHuiqian(index) {
      //通过下标index取药撤回会签的数据
      let revokeData = this.conSignList[index]
      this.$httploading({
        url: this.$http.adornUrl(`/caac/oa/component/workflow/workflowController/removeprocess`),
        method: 'POST',
        params: this.$http.adornParams({
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
          'data': this.$http.adornData({
            'removeWorkitemIds': this.paramObject.param01.workitemId,
            'configId': this.paramObject.param01.configId,
            'subProInstId': revokeData.subProInstId,
            'proDirId': this.paramObject.param01.proDirId,
            'proInstId': revokeData.proInstId,
            'actInstId': this.paramObject.param01.actInstId,
            'receiverId': revokeData.receiverId,
            'gongwenId': this.fwId
          })
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.$toast(data.data.message)
          uni.redirectTo({
            url: '/pages/admin/send/index?proInstId=' + this.proInstId
          })
        } else {
          this.$toast(data.data.message)
        }
      })

    },

    /**
     * 恢复会签
     * @param index 子组件传过来的下标，直接通过下标取对应的数据
     */
    recoverHuiqian(index) {
      let recoverData = this.conSignList[index]
      this.$httploading({
        url: this.$http.adornUrl(`/caac/oa/component/workflow/workflowController/reviveprocess`),
        method: 'POST',
        params: this.$http.adornParams({
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
          'data': this.$http.adornData({
            'proInstId': recoverData.subProInstId,
            'proDirId': this.paramObject.param01.proDirId,
            'configId': this.paramObject.param01.configId,
            'receiverId': recoverData.receiverId,
            'gongwenId': this.fwId,
            'actDefId': this.paramObject.param01.actDefId,
            'workItemId': this.paramObject.param01.workitemId,
            'actInstId': this.paramObject.param01.actInstId,
          })
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.$toast(data.data.message)
          uni.redirectTo({
            url: '/pages/admin/send/index?proInstId=' + this.proInstId
          })
        } else {
          this.$toast(data.data.message)
        }

      })
    },


    //获取签批功能菜单optionBtn01
    openwfneededdata(fwId, proInstId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/comm/comm/openwfneededdata`),
        method: 'get',
        params: this.$http.adornParams({
          'id': fwId,//发文详情返回的fwId
          'proInstId': proInstId,//发文详情返回的proInstId
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.paramObject.param01 = data.data
          this.getRoleTypesAndActInst(data.data.configId, proInstId)
        }

      })
    },
    //获取签批功能菜单optionBtn02
    getRoleTypesAndActInst(configId, proInstId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/wfm/ApplicationController/getRoleTypesAndActInst`),
        method: 'post',
        params: this.$http.adornParams({
          'proInstId': proInstId,//发文详情返回的proInstId
          'configId': configId,//openwfneededdata()返回的configId
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.paramObject.param02 = data.data
        }

      })
    },
    //获取发文待办详情数据
    getCaacDetail(proInstId) {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/authority/gljfw/proInstId/` + proInstId),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.userInfo.systemKey,//固定值
          'systemFlag': this.userInfo.systemFlag,//固定值
        })
      }).then(({data}) => {
        if (data.code == 200) {
          this.fwForm = data.data
          this.openwfneededdata(data.data.fwId, proInstId)
        }
      })
    },

  },
}
</script>

<style scoped>
	.main-container {
		background-color: #F4F4F4;
	}

	.van-top {
		border-bottom: 4rpx solid #0000fe;
		color: #0000fe;
		font-size: 30rpx;
		font-weight: 800;
	}

	.uni-table-th {
		color: #323232;
	}

	.btn-container {
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		width: 100%%;
		padding: 0 80rpx;
		justify-content: center;
	}

	.btn-container .top-btn {
		border-radius: 10rpx;
		margin-top: 30rpx;
		width: 100%;
		margin-top: 40rpx;
	}

	.bottom-btn {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		margin: 50rpx 0 100rpx;
	}

	.bottom-btn .left-btn {
		border-radius: 10rpx;
		width: 45%;
	}

	.bottom-btn .right-btn {
		border-radius: 10rpx;
		width: 45%;
	}
</style>
